package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.service.IRegionService;
import com.mdd.admin.validate.RegionCreateValidate;
import com.mdd.admin.validate.RegionUpdateValidate;
import com.mdd.admin.validate.RegionSearchValidate;
import com.mdd.common.base.page.PageValidate;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.Region;
import com.mdd.common.mapper.RegionMapper;
import com.mdd.system.vo.RegionDetailVo;
import com.mdd.system.vo.RegionListedVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import javax.annotation.Resource;
import java.util.*;

/**
 * 地区实现类
 * @author LikeAdmin
 */
@Service
public class RegionServiceImpl implements IRegionService {

    @Resource
    RegionMapper regionMapper;

    /**
     * 地区列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<RegionListedVo>
     */
    @Override
    public PageResult<RegionListedVo> list(PageValidate pageValidate, RegionSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<Region> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        regionMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "like:areaName@area_name:str",
            "=:parentId@parent_id:long",
            "=:level:long",
        });

        IPage<Region> iPage = regionMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<RegionListedVo> list = new LinkedList<>();
        for(Region item : iPage.getRecords()) {
            RegionListedVo vo = new RegionListedVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 地区详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return Region
     */
    @Override
    public RegionDetailVo detail(Long id) {
        Region model = regionMapper.selectOne(
                new QueryWrapper<Region>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "地区详情数据不存在");

        RegionDetailVo vo = new RegionDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 地区新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Transactional
    @Override
    public void add(RegionCreateValidate createValidate) {
        Region model = new Region();
        model.setAreaName(createValidate.getAreaName());
        model.setParentId(createValidate.getParentId());
        model.setLevel(createValidate.getLevel());
        regionMapper.insert(model);
    }

    /**
     * 地区详情
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Transactional
    @Override
    public void edit(RegionUpdateValidate updateValidate) {
        Region model = regionMapper.selectOne(
                new QueryWrapper<Region>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "地区数据不存在!");

        model.setId(updateValidate.getId());
        model.setAreaName(updateValidate.getAreaName());
        model.setParentId(updateValidate.getParentId());
        model.setLevel(updateValidate.getLevel());
        regionMapper.updateById(model);
    }

    /**
     * 地区删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Transactional
    @Override
    public void del(Long id) {
        Region model = regionMapper.selectOne(
                new QueryWrapper<Region>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "地区数据不存在!");

        regionMapper.delete(new QueryWrapper<Region>().eq("id", id));
    }

    @Override
    public List<RegionListedVo> listFirst() {
      QueryWrapper queryWrapper=   new QueryWrapper<Region>()
               .select("id,area_name,parent_id,level")
                .eq("level", 1);
      return regionMapper.selectList(queryWrapper);
    }
    @Override
    public List<RegionListedVo> listByParentId(Long parentId) {
        QueryWrapper queryWrapper=   new QueryWrapper<Region>()
                .select("id,area_name,parent_id,level")
                .eq("parent_id", parentId);
        return regionMapper.selectList(queryWrapper);
    }
}
